<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        let arr = [
            { id: "1", value: "xxx" },
            { id: "2", value: "xxx" },
            { id: "3", value: "xxx" },
            { id: "4", value: "xxx" },
            { id: "6", value: "xxx" },
            { id: "5", value: "xxx" },
            { id: "7", value: "xxx" },
            { id: "9", value: "xxx" }
        ]

        // function  getObj(arr) {
        //     let obj = {}
        //     let arr1 = []
        //     let arr2 = []
        //     arr.forEach(item => {
        //         if(item.id === "1" || +item.id % 2 === 0) {
        //             arr1.push(item);
        //         }else {
        //             arr2.push(item);
        //         }
        //     })
        //     arr1.forEach((item, index) => item.value = arr1[index + 1])
        //     arr2.forEach((item, index) => item.value = arr2[index + 1])
        //     console.log(arr1);
        //     console.log(arr2);

        //     obj[arr1[0].id] = arr1[0].value
        //     obj[arr2[0].id] = arr2[0].value

        //    let obj1 = JSON.parse(JSON.stringify(obj).replace(/value/g,"children"));

        //     return obj1;
        // }

        // function objKey(obj) {

        //     for(let key in obj) {

        //         if(obj[key] == 'undefined') {
        //             console.log(111);
        //             obj[key] = {};
        //             return;
        //         }else if(typeof obj[key] === "object") {
        //             console.log(222);
        //             objKey(obj[key]);
        //         }
        //     }
        //     return obj;

        // }
        function trans(data) {
            let dataSort = data.sort((a, b) => {
                console.log(a,'aaaa',b, 'bbbb');
                return a.id - b.id < 0 ? -1 : 1;
            });
            let tree;
            for (let i = 0; i < dataSort.length; i++) {
                let idNum = dataSort[i].id;
                let isEven = Number(idNum) % 2 === 0;
                if (tree === undefined) {
                    tree = {};
                    tree[1] = {};
                } else if (isEven) {
                    appendChildAttr(tree[1], idNum);
                } else {
                    if (tree[3] === undefined) {
                        tree[3] = {};
                    } else {
                        appendChildAttr(tree[3], idNum);
                    }
                }
                function appendChildAttr(parNode, childrenId) {
                    if (Object.keys(parNode).length === 0) {
                        parNode.children = {};
                        parNode.children[childrenId] = {};
                    } else {
                        appendChildAttr(parNode.children[Object.keys(parNode.children)[0]], childrenId);
                    }
                }
            }
            return tree;
        }

        console.log(trans(arr));
    </script>
</body>

</html>